//
//  ZLFocusView.m
//  ZLCamera
//
//  Created by 科大 on 2024/4/19.
//

#import "ZLFocusView.h"
#import "ZLFocusHintView.h"

@interface ZLFocusView()
/// 当前焦距比例系数
@property (nonatomic, assign) CGFloat currentZoomFactor;
/// 缩放回调
@property (nonatomic, copy) FocusPinchBeginCallBack begin;
/// 缩放回调
@property (nonatomic, copy) FocusPinchCallBack change;
@end

@implementation ZLFocusView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapFocusing:)];
        [self addGestureRecognizer:tap];
        UIPinchGestureRecognizer  *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchFocalLength:)];
        [self addGestureRecognizer:pinch];
    }
    return self;
}

#pragma mark - action
/// 聚焦手势
- (void)tapFocusing:(UITapGestureRecognizer *)tap {
    CGPoint point = [tap locationInView:self];
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:ZLFocusHintView.class]) {
            [subView removeFromSuperview];
        }
    }
    ZLFocusHintView *focusHintView = [[ZLFocusHintView alloc] initWithFrame:CGRectMake(point.x - 30, point.y - 30, 60, 60)];
    [self addSubview:focusHintView];
    focusHintView.transform = CGAffineTransformMakeScale(1.3, 1.3);
    [UIView animateWithDuration:0.5 animations:^{
        focusHintView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [focusHintView removeFromSuperview];
    }];
    
    if (self.focusTap) {
        self.focusTap(point);
    }
}

//调节焦距 手势
- (void)pinchFocalLength:(UIPinchGestureRecognizer *)pinch {
    if (pinch.state == UIGestureRecognizerStateBegan) {
        if (self.begin) {
            self.begin();
        }
    } else if (pinch.state == UIGestureRecognizerStateChanged) {
        if (self.change) {
            self.change(pinch.scale);
        }
    }
}

- (void)pinch:(FocusPinchBeginCallBack)begin change:(FocusPinchCallBack)change {
    self.begin = begin;
    self.change = change;
}

#pragma mark - init

@end
